/** @brief Test opening and closing a cursor on a table, running IUDS with the open cursor. 
 *
 *  testTableCursor.cpp
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 11..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */

#include "gtest/gtest.h"

#include "kangmodb.h"
#include "testCommon.h"


/** @brief Test ISUD on a single table.]
 */
class TableCursorTest : public testing::Test {
protected:  // You should make the members protected s.t. they can be
	// accessed from sub-classes.
	
	// virtual void SetUp() will be called before each test is run.  You
	// should define it if you need to initialize the varaibles.
	// Otherwise, this can be skipped.
	virtual void SetUp() {
		ASSERT_TRUE( setup_cursor( &db_, &tx_, &tableId_, &cursor_ ) == 0 );
	}
	
	// virtual void TearDown() will be called after each test is run.
	// You should define it if there is cleanup work to do.  Otherwise,
	// you don't have to provide it.
	//
	virtual void TearDown() {
		// close table succeeds
		ASSERT_TRUE( teardown_cursor( &db_, &tx_, &tableId_, &cursor_ ) == 0 );
	}
protected:
	stgKangmoDB * db_;
	stgTransaction * tx_;
	stgTableId tableId_;
	stgTableCursor * cursor_;
};

/** @brief Test Insert/Update/Delete/Select on a table.
 */
TEST_F(TableCursorTest, DISABLED_CursorIUDS) {
	char key[256];
	char value[256];

	stgData selectedValue;

	// insert 16 values with kmkim0dd
	for (int i=0; i<16; i++)
	{
		sprintf( key, "kmkim%03d", i);
		sprintf( value, "loves hyunsook %03d times", i);
		ASSERT_TRUE( cursor_->insertRecord( stgKey(key), KD_STR_DATA(value) ) == 0 );
	}
	
	// verify 16 values with kmkim0dd
	verify_key_values(cursor_, 16, "kmkim%03d", "loves hyunsook %03d times");
					
	// insert 16 values with hscho0dd
	for (int i=0; i<16; i++)
	{
		sprintf( key, "hscho%03d", i);
		sprintf( value, "loves kangmo %03d times", i);
		ASSERT_TRUE( cursor_->insertRecord( stgKey(key), KD_STR_DATA(value) ) == 0 );
	}

	// verify 16 values with hscho0dd
	verify_key_values(cursor_, 16, "hscho%03d", "loves kangmo %03d times");
									
	// update 16 values with kmkim0dd
	for (int i=0; i<16; i++)
	{
		sprintf( key, "kmkim%03d", i);
		sprintf( value, "loves and likes hyunsook %03d times", i);
		ASSERT_TRUE( cursor_->updateRecord( stgKey(key), KD_STR_DATA(value) ) == 0 );
	}

	// verify 16 values with kmkim0dd
	verify_key_values(cursor_, 16, "kmkim%03d", "loves and likes hyunsook %03d times");

	// delete 16 keys with kmkim0dd 
	for (int i=0; i<16; i++)
	{
		sprintf( key, "kmkim%03d", i);
		ASSERT_TRUE( cursor_->deleteRecord( stgKey(key) ) == 0 );
	}
					
	// verify the deleted 16 keys do not exist.
	for (int i=0; i<16; i++)
	{
		stgData selectedData; 
		sprintf( key, "kmkim%03d", i);
		ASSERT_TRUE( cursor_->seekRecord( stgKey(key), & selectedData ) == 0 );
		// if the key does not exist, stgValue::getValue() returns NULL
		ASSERT_TRUE( selectedData.getData() == NULL );
	}
									
}

/** @brief Test opening multiple cursors on a table.
 */
TEST_F(TableCursorTest, DISABLED_MultiCursor) {

}